import logging

import pytest


class Test:

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1.进入桌面，长按任意应用拖动到另一个图标上')
        device.refresh_layout()
        badges0 = device.get_elements_by_type('Badge')
        x0, y0 = device.center_of_element(badges0[0])
        x1, y1 = device.center_of_element(badges0[1])
        device.drag(x0, y0, x1, y1)
        logging.info('预期结果1.桌面生成一个合并文件夹')
        device.refresh_layout()
        device.assert_text_exist('新建文件夹1')
        badges1 = device.get_elements_by_type('Badge')
        assert len(badges1) - len(badges0) == 1, '文件夹不存在'
        logging.info('步骤2.长按合并文件夹在当前页拖动5次以上（空白页拖动未实现）')
        bx, by = device.center_of_element(device.get_element_by_text('新建文件夹1'))
        device.drag(bx, by - 100, 149, 540)
        device.drag(149, 540, 149, 813)
        device.drag(149, 813, 280, 813)
        device.drag(280, 813, 427, 813)
        device.drag(427, 813, 570, 813)
        device.drag(570, 813, 685, 813, 200)
        logging.info('预期结果2.拖动后界面显示正常，不会出现卡滞、残留文件夹')
        device.refresh_layout()
        # badges2 = device.get_elements_by_type('Badge')
        # assert len(badges2) == len(badges1), '有残留文件夹'
        folder_title = device.get_elements_by_text('新建文件夹1')
        nbx, nby = device.center_of_element(device.get_element_by_text('新建文件夹1'))
        device.swipe(100, 640, 710, 640)
        device.refresh_layout()
        device.assert_text_exist('新建文件夹1', expect=False)
        assert len(folder_title) == 1, '有残留文件夹'
        assert (bx, by) != (nbx, nby), '文件夹拖动失败'
